package cn.edu.xmu.oomall.customer.service;


import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.oomall.customer.dao.CouponDao;
import cn.edu.xmu.oomall.customer.dao.bo.Coupon;
import cn.edu.xmu.oomall.customer.dao.bo.CouponAct;
import cn.edu.xmu.oomall.customer.dao.openfegin.CouponActDao;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(propagation = Propagation.REQUIRED)
@RequiredArgsConstructor
public class CouponService {

    private final CouponActDao couponActDao;

    private final CouponDao couponDao;

    public void claimCoupon(Long actId, Long customerId, UserDto user) {
        CouponAct act = couponActDao.findById(actId);
        act.claimCoupon(customerId,user);
    }

    public List<Coupon> retrieveCoupons(Long used, UserDto user, Integer page, Integer pageSize) {
       return couponDao.retrieveCoupons(used,user, page, pageSize).stream().map(bo -> {
            bo.setCouponAct(couponActDao.findById(bo.getActivityId()));
            return bo;
        }).collect(Collectors.toList());
    }
}
